#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    int a,b,n;
    while(cin>>a>>b>>n)
    {
        if(a==0 && b==0 && n==0) break;
        if(n==1 || n==2) cout<<"1"<<endl;//注意n的取值
        else
        {
        int f1=a,f2=b,f3=1,f4=0;//快速幂算矩阵
        int t1,t2,t3,t4;         //矩阵表示  (1,3) (f1,f3)
        int r1=1,r2=0,r3=0,r4=1; //          (2,4) (f2,f4)
        n=n-2;
        for(;n;n >>=1)
        {
            if(n & 1)
            {
                t1=(r1*f1+r3*f2)%7;
                t2=(r2*f1+r4*f2)%7;
                t3=(r1*f3+r3*f4)%7;
                t4=(r2*f3+r4*f4)%7;
                r1=t1;
                r2=t2;
                r3=t3;
                r4=t4;
            }
            t1=(f1*f1+f3*f2)%7;
            t2=(f2*f1+f4*f2)%7;
            t3=(f1*f3+f3*f4)%7;
            t4=(f2*f3+f4*f4)%7;
            f1=t1;
            f2=t2;
            f3=t3;
            f4=t4;

        }
        cout<<(r1+r2)%7<<endl;
        }
    }
    return 0;
}
